<!DOCTYPE html>
<html lang="en-us" id="extr-page">
<head>
<meta charset="UTF-8">
	<link rel="stylesheet" type="text/css" media="screen" href="../../css/bootstrap.min.css">
	<link rel="stylesheet" href="../../css/ztree/zTreeStyle/zTreeStyle.css" type="text/css">
	<link rel="stylesheet" href="../../css/ztree/demo.css" type="text/css">
</head>
<body>
<div class="col-xs-12 col-sm-12 col-md-6 col-lg-6 col-md-offset-2 col-lg-offset-2">
	<form class="form-horizontal" onsubmit="return false" id="form">
		<fieldset>
			<input type="hidden" id="id" name="id">
			<div class="form-group">
				<label class="col-md-4 control-label">角色</label>
				<div class="col-md-8">
					<input class="form-control" placeholder="角色" type="text" name="name" id="name"
						data-bv-notempty="true"
						data-bv-notempty-message="角色 不能为空">
				</div>
			</div>
			<div class="form-group">
				<label class="col-md-4 control-label">模块</label>
				<div class="col-md-8">
					<select class="form-control input-sm" name="modular" id="modular"
							data-bv-notempty="true" data-bv-notempty-message="模块 不能为空">
					</select>
				</div>
			</div>
			<div class="form-group">
				<label class="col-md-4 control-label">数据范围</label>
				<div class="col-md-8">
					<select class="form-control input-sm" name="dataScope" id="dataScope">
						<option value="1">所有数据</option>
						<option value="4">所在部门及以下数据</option>
						<option value="5">所在部门数据</option>
						<option value="8" selected>仅本人数据</option>
					</select>
				</div>
			</div>
			<div class="form-group">
				<label class="col-md-4 control-label">是否可用</label>
				<div class="col-md-8">
					<select class="form-control input-sm" name="usable" id="usable">

					</select>
				</div>
			</div>
			<div class="form-group">
				<label class="col-md-4 control-label">描述</label>
				<div class="col-md-8">
					<textarea class="form-control" placeholder="描述" name="description" id="description"></textarea>
				</div>
			</div>
			<div class="form-group">
				<label class="col-md-4 control-label">权限</label>
				<div class="col-md-8">
					<ul id="treeDemo" class="ztree"></ul>
				</div>
			</div>
			<div class="form-actions">
				<div class="row" align="center">
					<div class="col-md-12">
						<!--<button class="btn btn-primary" onclick="location.href='roleList.html'">返回</button>-->
						<button class="btn btn-primary" type="submit" onclick="save()">
							<i class="fa fa-save"></i> 保存
						</button>
					</div>
				</div>
			</div>

		</fieldset>
	</form>
</div>

	<script type="text/javascript" src="../../js/libs/jquery-2.1.1.min.js"></script>
	<script type="text/javascript" src="../../js/jq.js"></script>
	<script type="text/javascript" src="../../js/plugin/bootstrapvalidator/bootstrapValidator.min.js"></script>
	<script type="text/javascript" src="../../js/common.js"></script>
	<script type="text/javascript" src="../../layui/layui.js"></script>
	<script type="text/javascript" src="../../js/libs/jquery.ztree.all-3.5.min.js"></script>
	<script type="text/javascript" src="../../js/my/ztree-menu.js"></script>
	<script type="text/javascript" src="../../js/dict.js"></script>
	<script type="text/javascript">
		layui.use('layer', function(){
			var layer = layui.layer;
		});

        showDictSelect('usable', 'boolean', '', '1');
		showDictSelect('modular', 'modular', '', '');

		var id = getUrlParam('id');
		var treeId = 'treeDemo';

		if (id) {
            initData(id);
            initTree(treeId, '', 'check', '/permissions/all', '/permissions?roleId=' + id, true, 'root');
        } else {
            initTree(treeId, '', 'check', '/permissions/all');
		}

		function initData(_id){
			if(_id){
				$.ajax({
					type : 'get',
					url : '/roles/' + _id,
					async : false,
					success : function(data) {
                        $('#id').val(data.id);
                        $('#name').val(data.name);
                        $('#description').val(data.description);
                        $('#dataScope').val(data.dataScope);
                        $('#usable').val(data.usable);
                        $('#modular').val(data.modular);
					}
				});
			}
		}
		
		
		$('#form').bootstrapValidator();
		
		function save() {
			var bootstrapValidator = $("#form").data('bootstrapValidator');
			bootstrapValidator.validate();
		    if(!bootstrapValidator.isValid()){
			   return;
		    }
		    
		    var formdata = $("#form").serializeObject();
		    formdata.permissionIds = getTreeCheckedIds(treeId);

			$.ajax({
				type : 'post',
				url : '/roles',
				contentType: "application/json; charset=utf-8",  
				data : JSON.stringify(formdata),
				success : function(data) {
					layer.msg("成功", {shift: -1, time: 1000}, function(){
						parent.location.reload();//关闭iFrame
                    });
				}
			});
		}
		
	</script>
</body>
</html>